<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use App\User;
use DB;
use Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     * 加载用户中心
     */
    public function index()
    {
        return view("home.user.user");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     * 用户注册
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $token=str_random(50);//随机的50长度字符串用于邮箱
        $createtime=time();//注册时间,提取当前时间戳

        $arr=Array();//数组用于储存发送数据库信息
        $arr['password']=Hash::make($data['password']);//使用哈希加密密码
        $arr['token']=$token;
        $arr['createtime']=$createtime;
        $arr['tel']=$data['phone'];

        $res=DB::table("user")->insertGetId($arr);//发送到数据库

        $arr2=Array();
        $arr2['uid']=$res;
        $arr2['nickname']=$data['phone'];

        $res2=DB::table("userdetails")->insertGetId($arr2);//发送到数据库


        return redirect("/");
    }

    /**
     * Display the specified resource.
     * 加载个人中心
     */
    public function show($id)
    {
        $id=substr($id,0,-2);
        $user=\DB::table("user")->where("id",$id)->first();
        $userdetails=\DB::table("userdetails")->where("uid",$id)->first();

         //热销
        $hotgoods =\DB::table('orders')
                     ->select(\DB::raw('goods.*,orders.gid,COUNT(orders.gid) AS counts'))
                     ->leftJoin('goods', 'goods.id', '=', 'orders.gid')
                     ->groupBy('orders.gid')
                     ->orderBy('counts', 'desc')
                     ->limit(5)
                     ->get();

     //代付款
    $witepay=\DB::table("orders")
                ->where('uid','=',$id)
                ->where("status",1)
                ->count();
    //待评价
    $comment=\DB::table("orders")
                ->where('uid','=',$id)
                ->where("comment",0)
                ->count();


        return view("home.user.userhome",["id"=>$id,"user"=>$user,"userdetails"=>$userdetails,"hotgoods"=>$hotgoods,"witepay"=>$witepay,"comment"=>$comment]);
    }

    /**
     * Show the form for editing the specified resource.
     * 退出登录
     */
    public function edit($id)
    {
         $id=substr($id,0,-2);
         \Cookie::queue('user',"",-1);
         \Cookie::queue('userdetails',"",-1);
         return redirect("/");
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     *  用户注册
     *  加载注册模板
     */
    public function login()
    {

        return view("home.user.userLogin");
    }

    /**
     * 用户手机号验证码
     * 发送手机号
     * 成功返回验证码,失败返回0
     */
    public function phone(Request $request)
    {
        $phone = $request->input("phone");
        $res=\Redis::get("STRING:PHONE:CODE".$phone);
        if($res){
            return 1;
        }

        $code = rand(1000,9999);

        session(['phone'=>$code]);
        $url = 'http://106.ihuyi.com/webservice/sms.php?method=Submit&account=C59392015&password=bc19a6e4889cbfa22a5e6f2515226695&format=json&mobile='.$phone.'&content=您的验证码是：'.$code.'。请不要把验证码泄露给其他人。';
         $ch = curl_init();
        // 添加apikey到header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);
        // 将json转化为数组
        $arr = json_decode($res,true);
        //判断是否发送成功,成功返回验证码
        if($arr['code']==2){
            \Redis::setex("STRING:PHONE:CODE".$phone,60,$code);

            return $code;
        }else{
            echo 0;
        }
        // echo 123;
    }

    /**
     *  判断手机号是否注册过
     *  书册返回0  未注册返回1
     */
    public function phoneLogin(Request $request)
    {
       $phone = $request->input("phone");//提取ajax发送来的手机号码

       $res=DB::table('user')->where("tel",$phone)->first();

       if($res){
        echo 0;
       }else{
        echo 1;
       };
   }

   /**
    *
    *     验证登录账号密码是否正确
    *     成功跳转到首页
    *     失败重新登录,账号不刷新
    *
    **/
   public function doLogin(Request $request)
   {
    $account=$request->input("account");//接收账号名
    $password=$request->input("password");//接收密码

    $no=DB::table("user")->where("tel",$account)->orWhere("email",$account)->first();//判断账号是不是邮箱或手机号

    $id=$no['id'];
    //判断账号和密码

    if($no&&Hash::check($password,$no['password'])){

        \Cookie::queue('user',$no,60);
        $data=\DB::table("userdetails")->where("uid",$id)->first();

        \Cookie::queue('userdetails',$data,60);

       return redirect('/');

    }else{
        $request->flashOnly("account");
        echo $password."<br>";
        echo $no['password'];
        return redirect('/user')->with('error','密码错误');
    }


   }
}
